﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Services.Interface;
using HanGu.Entity;
using HanGu.Common;

namespace Hangu.WebClient.Controllers.Admin
{
    public class CultureEducationController : AdminBaseController
    {
        private static readonly ICultureEducation serviceEducation = ServiceFactory.GetService<ICultureEducation>();
         
        //
        // GET: /CultureEducation/

        public ActionResult Index()
        {
            return View(viewList, GetCultureEducationList());
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            
            SearchParma.Pagination.RowCount = null;
            return View(viewList, GetCultureEducationList());
        }

        List<CultureEducation> GetCultureEducationList()
        {
            List<CultureEducation> CultureEducationList = serviceEducation.GetAll(base.SearchParma);
            ViewData["RowCount"] = SearchParma.Pagination.RowCount;
            return CultureEducationList;
        }

        public ActionResult Create(int id)
        {
            CultureEducation CultureEducation = new CultureEducation() { GUID = Helper.GetGuid, CreateTime = DateTime.Now, Creator = CurentUser.UserName, UpdateTime = DateTime.Now, Updater = CurentUser.UserName };
            if (id > 0)
            {
                CultureEducation = serviceEducation.GetByID(id);
            }
            if (!CultureEducation.IsIndex.HasValue)
            { CultureEducation.IsIndex = false; }
            if (string.IsNullOrEmpty(CultureEducation.TitlePic))
            {
                CultureEducation.TitlePic = "/Content/default.jpg";
            }
            return View(viewCreate, CultureEducation);
        }

        //
        // POST: /CultureEducation/Create

        [HttpPost]
        [ValidateInput(false)]
        public string Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                CultureEducation CultureEducation = new CultureEducation();
                TryUpdateModel(CultureEducation, collection);
                CultureEducation.IsIndex = collection["IsIndex"] != null;
                if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
                {
                    if (Request.Files[0].ContentType.IndexOf("image") >= 0)
                    {
                        UploadFileManage uploadFile = new UploadFileManage(Request.Files[0]);
                        uploadFile.ClassType = navTabId;
                        uploadFile.Save();
                        if (!uploadFile.Error)
                        {
                            //先删除旧图片
                            if (!string.IsNullOrEmpty(CultureEducation.TitlePic))
                            {
                                uploadFile.DeleteFile(CultureEducation.TitlePic);
                            }
                            CultureEducation.TitlePic = uploadFile.FileInfo["filepath"];
                            if (CultureEducation.ID > 0)
                            {
                                serviceEducation.Update(CultureEducation);
                            }
                            else
                            {
                                serviceEducation.Add(CultureEducation);
                            }
                        }
                        else
                        {
                            ajaxStr = string.Format(ajaxStr, 300, uploadFile.Message, navTabId, "", "");
                        }
                    }
                    else
                    { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.UnusefulImag, navTabId, "", ""); }
                }
                else
                {
                    if (CultureEducation.ID > 0)
                    {
                        serviceEducation.Update(CultureEducation);
                    }
                    else
                    {
                        serviceEducation.Add(CultureEducation);
                    }

                }
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");

            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }

        //
        // GET: /CultureEducation/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /CultureEducation/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /CultureEducation/Delete/5
        [HttpPost]
        public string Delete(int id, string guid)
        {
            try
            {
                try
                {
                    FileManageController filemanage = new FileManageController();
                    filemanage.DeleteExecute(guid);
                }
                catch { }
                serviceEducation.Delete(id);
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";

        }
    }
}
